package com.yxkj.syh.app.huarong.activities.user;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.launcher.ARouter;
import com.syh.app.basic.App;
import com.syh.app.basic.base.BaseResponse;
import com.syh.app.basic.base.view_model.BaseViewModel;
import com.syh.app.basic.bindingAdp.view_click.ClickEvent;
import com.syh.app.basic.config.BasicConstants;
import com.syh.app.basic.utils.AppUtil;
import com.syh.app.basic.utils.ComUtil;
import com.syh.app.basic.utils.DateUtils;
import com.syh.app.basic.utils.Tooast;
import com.syh.app.basic.utils.net.ApiObserver;
import com.trello.rxlifecycle2.android.FragmentEvent;
import com.yxkj.syh.app.huarong.bean.AppointmentResponse;
import com.yxkj.syh.app.huarong.bean.ReportResponse;
import com.yxkj.syh.app.huarong.bean.ReturnCarsResponse;
import com.yxkj.syh.app.huarong.bean.UserInfoResponse;
import com.yxkj.syh.app.huarong.constants.ArouterPath;
import com.yxkj.syh.app.huarong.data_center.model.AccountModel;
import com.yxkj.syh.app.huarong.data_center.model.AuthModel;
import com.yxkj.syh.app.huarong.data_center.model.ReportModel;
import com.yxkj.syh.app.huarong.util.UserManager;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 创建者：SYH
 * 时间：2020/5/20 17:26
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class UserVM extends BaseViewModel<FragmentEvent> {
    public UserVM(@NonNull Application application) {
        super(application);
    }

    public ObservableField<String> ofName = new ObservableField<>();
    public ObservableField<String> ofVer = new ObservableField<>(AppUtil.getAppVersionName(App.getApplication()));

    public ObservableField<String> ofCarNo = new ObservableField<>();
    public ObservableField<String> ofCount1 = new ObservableField<>();
    public ObservableField<String> ofCount2 = new ObservableField<>();
    public ObservableField<String> ofCount3 = new ObservableField<>();

    public MutableLiveData<Boolean> mldLogout = new MutableLiveData<>();


    public ClickEvent agreementsClick = new ClickEvent() {
        @Override
        public void onClick(View view) {
            ARouter.getInstance().build(ArouterPath.WEB_ACTIVITY)
                    .withString("title", "")
                    .withString("link", BasicConstants.USER_AGREEMENT_URL)
                    .navigation();
        }
    };
    public ClickEvent privacyClick = new ClickEvent() {
        @Override
        public void onClick(View view) {
            ARouter.getInstance().build(ArouterPath.WEB_ACTIVITY)
                    .withString("title", "")
                    .withString("link", BasicConstants.PRIVACY_CLAUSE_URL)
                    .navigation();
        }
    };

    public void userInfo() {
        AccountModel.getAccountModel().userInfo(mLifecycleProvider.bindToLifecycle(), new ApiObserver<UserInfoResponse>() {
            @Override
            public void onSuccess(UserInfoResponse userInfoResponse) {
                UserManager.getUserManager().saveUser(userInfoResponse.getData());
                ofName.set(userInfoResponse.getData().getRealName());
                if (!userInfoResponse.getData().getEmpId().equals(BasicConstants.WAIDIAOSIJI_EMPID)) {
                    ofCarNo.set(userInfoResponse.getData().getCarNo());
                }
            }

            @Override
            public void onFailed(int code, String msg) {
                Tooast.warning(msg);
            }
        });
    }


    public void report() {
        Map<String, Object> pMap = new HashMap<>();
        pMap.put("start", "1990-01-01 00:00:00");
        pMap.put("end", DateUtils.getFormatTime(null, new Date()));
        ReportModel.getReportModel().report(pMap, mLifecycleProvider.bindToLifecycle(), new ApiObserver<ReportResponse>() {
            @Override
            public void onSuccess(ReportResponse reportResponse) {
                ReportResponse.Report report = reportResponse.getData();
                ofCount1.set(report.getLogisticsNumber() + "");
                ofCount2.set(ComUtil.clearNoUseZero(report.getLogisticsTotalWeight()).toPlainString());
            }

            @Override
            public void onFailed(int code, String msg) {
                Tooast.warning(msg);
            }
        });
    }

    public void logout() {
        AuthModel.getAuthModel().logout(mLifecycleProvider.bindToLifecycle(), new ApiObserver<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                mldLogout.setValue(true);
            }

            @Override
            public void onFailed(int code, String msg) {
                mldLogout.setValue(true);
                Tooast.warning(msg);
            }
        });
    }

}
